//
//  tableViewController.m
//  LCSJieMianApp
//
//  Created by 千锋 on 15/12/21.
//  Copyright (c) 2015年 1000phone. All rights reserved.
//

#import "tableViewController.h"
#import "ScrollViewCell.h"
#import "NewsViewCell.h"
#import "LCSNetworkingManager.h"
#import "ScrollmageDataModel.h"
#import "firstTableViewModel.h"
#import "SubViewController.h"

#define TABLEVIEW_TAG 100
@interface tableViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSMutableArray *_dataArray;
}

@property (strong, nonatomic)LCSNetworkingManager *manager;
@end

@implementation tableViewController

- (LCSNetworkingManager *)manager
{
    if (!_manager) {
        _manager = [LCSNetworkingManager manager];
    }
    return _manager;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
//    [self requestData];
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.userInteractionEnabled = YES;
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerNib:[UINib nibWithNibName:@"NewsViewCell" bundle:nil] forCellReuseIdentifier:@"NewsViewCell"];
    [_tableView setSeparatorColor:[UIColor clearColor]];
    [self.view addSubview:_tableView];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 重写init方法
- (void)setUrl:(NSString *)url
{
    _url = url;
    [self requestData];
}
- (void)requestData
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
     }
    if (self.url.length != 0) {
        NSString *requestUrl = [NSString stringWithString:self.url];
        
        if (self.number == TABLEVIEW_TAG) {
            requestUrl = [NSString stringWithFormat:@"%@?page=1",self.url];
        }else
        {
            
        }
        
        [self.manager GET:requestUrl success:^(NSURLResponse *response, NSData *data) {
            NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableDictionary *resultDict = [[NSMutableDictionary alloc]init];
            resultDict = [responseDict objectForKey:@"result"];
            
            
            NSArray *imageArray = [[NSArray alloc] init];
            imageArray = [resultDict objectForKey:@"carousel"];
            
            if (imageArray.count > 0) {
                // 首页数据中的scrolView的数据
                NSMutableArray *ScrolImageArr = [[NSMutableArray alloc] init];
                
                for (NSDictionary *dict in imageArray) {
                    firstTableViewModel *model = [[firstTableViewModel alloc] init];
                    [model setValuesForKeysWithDictionary:dict];
                    [ScrolImageArr addObject:model];
                }
                [_dataArray addObject:ScrolImageArr];
                
                NSArray *rstArray = [[NSArray alloc] init];
                rstArray = [resultDict objectForKey:@"rst"];
                NSDictionary *rstDict = [[NSDictionary alloc] init];
                rstDict = [rstArray firstObject];
                NSArray *dataArr = [[NSArray alloc] init];
                // 首页其他数据
                dataArr = [rstDict objectForKey:@"data"];
                
                for (NSDictionary *dict in dataArr) {
                    firstTableViewModel *model = [[firstTableViewModel alloc] init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_dataArray addObject:model];
                }
                
                
            }else
            {
                
                NSArray *rstArray = [[NSArray alloc] init];
                rstArray = [resultDict objectForKey:@"rst"];
                
                for (NSDictionary *dict in rstArray) {
                    firstTableViewModel *model = [[firstTableViewModel alloc] init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_dataArray addObject:model];
                    
                }
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            });
        }];

    }else
        return;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
        
        if (self.number == TABLEVIEW_TAG){
            
            if (indexPath.row == 0) {
                ScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScrollViewCell"];
                if (cell == nil) {
                    cell = [[ScrollViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScrollViewCell"];
                }
                cell.imageNameArray = _dataArray[indexPath.row];
                
                return cell;
            }else
            {
                NewsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsViewCell" forIndexPath:indexPath];
                
                    cell.model = _dataArray[indexPath.row];
                    
                    return cell;
            }
        }else
        {
            
            NewsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsViewCell" forIndexPath:indexPath];
            
            cell.model = _dataArray[indexPath.row];
                
            return cell;
            
        }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.number == TABLEVIEW_TAG) {
        if (indexPath.row == 0) {
            return 250;
        }
        else
            return 230;
    }
    else
        return 230;
}

#pragma mark - 新闻详情

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubViewController *subVC = [[SubViewController alloc] init];
    [self.navigationController pushViewController:subVC animated:YES];
    [self.navigationController setNavigationBarHidden:YES];
}
@end
